//
//  DetailContactInfoVC.m
//  CreditHub
//
//  Created by chenglang on 2024/9/21.
//

#import "DetailContactInfoVC.h"
#import "ContactTextInputView.h"
#import "HomeRequest.h"

@interface DetailContactInfoVC ()
@property (weak, nonatomic) IBOutlet UIView *contentView;
@property (weak, nonatomic) IBOutlet UIImageView *imageTitle;
@property (weak, nonatomic) IBOutlet UILabel *labelTitle;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *layoutHeight;
@property (nonatomic , strong) NSMutableArray *arraySuchan;

@end

@implementation DetailContactInfoVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navBgImageView.hidden = NO;
    self.ch_title = @"Emergency contact";
    self.startTime = [BaseService getTimeStringSince1970];
    self.pointType = @"8";
    [self requestDetail];
    
}
- (void)requestDetail{
    __weak typeof(self) weakSelf = self;
    [HomeRequest requestCerContactslInfoId:self.detailId success:^(NSURLSessionDataTask *task, id response) {
        if([response[@"ofexcitement"] integerValue] == 0){
            weakSelf.arraySuchan = [NSMutableArray arrayWithArray:response[@"pity"][@"immediately"]];
            for(int i = 0 ; i < weakSelf.arraySuchan.count;i++){
                NSDictionary *dicInfo = weakSelf.arraySuchan[i];
                ContactTextInputView *view = [[NSBundle mainBundle] loadNibNamed:@"ContactTextInputView" owner:self options:@{}].firstObject;
                view.tag = 2000+i;
                view.dicInfo = [NSMutableDictionary dictionaryWithDictionary:dicInfo];
               
                [weakSelf.contentView addSubview:view];
                [view mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.mas_equalTo(15);
                    make.right.mas_equalTo(-15);
                    make.height.offset(230);
                    make.top.mas_equalTo(140+i*230);
                }];
            }
            weakSelf.layoutHeight.constant = 140 +20+  weakSelf.arraySuchan.count*230;
            
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}

- (IBAction)clickSubmit:(id)sender {
    [self.view endEditing:YES];
    NSMutableArray *array = [NSMutableArray array];
    for(int i = 0 ; i < self.arraySuchan.count;i++){
        ContactTextInputView *view = [self.contentView viewWithTag:2000+i];
        NSDictionary *dicInfo = view.dicInfo;

        [array addObject:@{
            @"wouldwonder":CHString(dicInfo[@"wouldwonder"]),
            @"quickas":CHString(dicInfo[@"quickas"]),
            @"whew":CHString(dicInfo[@"whew"])
            
        }];
        
    }
    
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:array options:0 error:&error];
    NSString *jsonString = @"";
    if (jsonData) {
        jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
       
    }
    __weak typeof(self) weakSelf = self;
    xhl_hudLoading();
    [HomeRequest requestSaveCerContactslInfoId:self.detailId pity:jsonString success:^(NSURLSessionDataTask *task, id response) {
        if([response[@"ofexcitement"] integerValue] == 0){
            [weakSelf.navigationController popViewControllerAnimated:YES];
            weakSelf.endTime = [BaseService getTimeStringSince1970];
            [weakSelf requestUploadPointType];
        }
        xhl_showMessage(response[@"shared"]);
      
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        xhl_hudDismiss();
    }];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
